﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using WebSite.App_Data;
using WebSite.App_Code;
using System.Security.Cryptography;
using Excel = Microsoft.Office.Interop.Excel;

namespace WebSite.App_Code
{
    #region Item.Common

    public class CommonItem
    {
        public CommonItem() { }

        public static List<list_ProjcetType> GetProjectType()
        {
            List<list_ProjcetType> listTemp = new List<list_ProjcetType>();

            DBDataContext DB = new DBDataContext();

            DB.Connection.Open();

            listTemp = (from x in DB.list_ProjcetTypes select x).ToList();

            DB.Connection.Close();

            return listTemp;
        }

        public static String X_yyyyMMdd(String Date)
        {
            DateTime Temp = new DateTime();
            if (DateTime.TryParse(Date, out Temp)) { return Temp.ToString("yyyy-MM-dd"); }
            else
                return Date;
        }

        public static Int32 X_Int32(String Int)
        {
            return X_Int32(Int, 0);
        }

        public static Int32 X_Int32(String Int, Int32 DefaultValue)
        {
            Int32 Temp = 0;
            if (Int32.TryParse(Int, out Temp)) { return Temp; }
            else
                return DefaultValue;
        }

        public static Decimal X_Decimal(String D)
        {
            Decimal Temp = 0;
            if (Decimal.TryParse(D, out Temp)) { return Temp; }
            else
                return 0;
        }

        public static Int32 X_Sum(List<String> S)
        {
            Int32 Tmp = 0;

            foreach(String One in S)
            {
                Tmp = Tmp + X_Int32(One);
            }

            return Tmp;
        }

        public static void WirteLog(DBDataContext DB, L_LogType Type, String LogTxt,String Key, String KeyDes, UserItem U)
        {

            if (U == null)
                return;

            DB.sys_Logs.InsertOnSubmit(new sys_Log()
            {
                UserGuid = U.User.Guid,
                UserName = U.User.UserName,
                LogDescription = LogTxt,
                OperationTime = DateTime.Now,
                LogType = (int)Type,
                KeyWord = Key,
                KeyDescription = KeyDes,
            });
        }

        public static void WirteErrorLog(L_LogType Type, Exception Ex, String Des, UserItem U)
        {
            DBDataContext DB = new DBDataContext();

            DB.Connection.Open();

            String UserGuid = String.Empty;
            String UserName = String.Empty;

            if (U != null)
            {
                UserGuid = U.User.Guid;
                UserName = U.User.UserName;
            }

            DB.sys_ErrorLogs.InsertOnSubmit(new sys_ErrorLog()
            {
                UserGuid = UserGuid,
                UserName = UserName,
                ExMessage = Ex.Message,
                ExSource = Ex.Source,
                ExStackTrace = Ex.StackTrace,
                LogDescription = Des,
                OperationTime = DateTime.Now
            });

            DB.SubmitChanges();

            DB.Connection.Close();
        }
    }
        
    #endregion

    #region Item.ExcelVar

    public class ExcelVar
    {
        public ExcelVar(Excel.Worksheet ws, Excel._Application application)
        {
            Conf = ws;
            App = application;
        }

        public Excel.Worksheet Conf { get; set; }

        public Excel._Application App { get; set; }

        public String Version { get { return Convert.ToString(Conf.get_Range("C3").Text); } }

        public String PjCategoryID { get { return Convert.ToString(Conf.get_Range("C7").Text); } }

        public String PjType { get { return Convert.ToString(Conf.get_Range("C8").Text); } }

        public String GetSheetVar(String Code, String Attribute)
        {
            String Value = String.Empty;

            if (App.WorksheetFunction.CountIf(Conf.get_Range("3:3"), "Code") > 0 && 
                App.WorksheetFunction.CountIf(Conf.get_Range("3:3"), Attribute) > 0)
            {
                int C_Code = (int)App.WorksheetFunction.Match("Code", Conf.get_Range("3:3"), 0);
                int C_Attribute = (int)App.WorksheetFunction.Match(Attribute, Conf.get_Range("3:3"), 0);

                if (App.WorksheetFunction.CountIf((Excel.Range)Conf.Columns[C_Code], Code) > 0)
                {
                    int R_Sheet = (int)App.WorksheetFunction.Match(Code, (Excel.Range)Conf.Columns[C_Code], 0);
                    Value = ((Excel.Range)Conf.Cells[R_Sheet, C_Attribute]).Text.ToString();
                }
            }

            return Value;
        }

        public void SetSheetVar(String Code, String Attribute, String Value)
        {
            if (App.WorksheetFunction.CountIf(Conf.get_Range("3:3"), "Code") > 0 &&
                App.WorksheetFunction.CountIf(Conf.get_Range("3:3"), Attribute) > 0)
            {
                int C_Code = (int)App.WorksheetFunction.Match("Code", Conf.get_Range("3:3"), 0);
                int C_Attribute = (int)App.WorksheetFunction.Match(Attribute, Conf.get_Range("3:3"), 0);

                if (App.WorksheetFunction.CountIf((Excel.Range)Conf.Columns[C_Code], Code) > 0)
                {
                    int R_Sheet = (int)App.WorksheetFunction.Match(Code, (Excel.Range)Conf.Columns[C_Code], 0);
                    ((Excel.Range)Conf.Cells[R_Sheet, C_Attribute]).Value2 = Value;
                }
            }
        }
    }

    #endregion

    #region Item.BgtPriItem

    public class BgtPriItem
    {
        public BgtPriItem()
        {
            DepGuid = String.Empty;
            BgtStatus = String.Empty;
            PriInt32 = 0;
        }

        public String DepGuid { get; set; }

        public String BgtStatus { get; set; }

        public Int32 PriInt32 { get; set; }
    }

    #endregion

    #region Item.User

    public class UserItem
    {
        public UserItem()
        {
            User = new md_User();
            Department = new DepartmentItem();
            PageList = new List<md_Page>();
            RoleList = new List<md_Role>();
            AppRoleList = new List<CheckRightItem>();
            HisRoleList = new List<CheckRightItem>();
            SubRoleList = new List<SubRoleItem>();
            //IsOnlyNormal = true;
            //PriList = new List<BgtPriItem>();
        }

        public md_User User { get; set; }

        //public Boolean IsOnlyNormal { get; set; }

        public DepartmentItem Department { get; set; }

        public List<md_Page> PageList { get; set; }

        public List<md_Role> RoleList { get; set; }

        public List<CheckRightItem> AppRoleList { get; set; }

        public List<CheckRightItem> HisRoleList { get; set; }

        public List<SubRoleItem> SubRoleList { get; set; }

        public bool HasRole(Role R)
        {
            return RoleList.Where(o => o.RoleID == (int)R).Any();
        }

        public Boolean AppCan(L_ActionType A, list_ProjectCategory PC, md_Department D, md_User U, list_BudgetStatus S)
        {
            IEnumerable<CheckRightItem> ieDep = from x in this.AppRoleList
                                                where x.Type == L_CheckType.Department
                                                where x.Action == A
                                                where (int)x.Status == Convert.ToInt32(S.Status)
                                                where (x.ProjectCtegory.ID == PC.ID || x.ProjectCtegory.Name == PC.Name)
                                                where (x.Department.Guid == D.Guid || x.Department.DepartmentName == D.DepartmentName)
                                                select x;

            IEnumerable<CheckRightItem> ieUser = from x in this.AppRoleList
                                                 where x.Type == L_CheckType.User
                                                 where x.Action == A
                                                 where (int)x.Status == Convert.ToInt32(S.Status)
                                                 where (x.ProjectCtegory.ID == PC.ID || x.ProjectCtegory.Name == PC.Name)
                                                 where (x.PIC.Guid == U.Guid || x.PIC.UserName == U.UserName)
                                                 select x;

            return (ieDep.Any() || ieUser.Any());
        }

        public Boolean AppCan(L_ActionType A, String PC, String D, String U, String S)
        {
            IEnumerable<String> ieWhere = (from o in this.AppRoleList
                                           where o.Action == A
                                           select String.Format("[{0}][{1}][{2}][{3}]", o.ProjectCtegory.ID, o.Department.Guid, o.PIC.Guid, (int)o.Status));
            if (ieWhere.Any())
            {
                return (ieWhere.ToList().Contains(String.Format("[{0}][{1}][{2}][{3}]", PC, D, String.Empty, S))
                    || ieWhere.ToList().Contains(String.Format("[{0}][{1}][{2}][{3}]", PC, String.Empty, D, S)));
            }
            else
                return false;
        }

        public List<String> GetQueryWhereByDep(Boolean His)
        {
            List<String> tmp = new List<string>();

            if (His)
            {
                IEnumerable<String> ieWhere = (from o in this.HisRoleList
                                               where o.Action == L_ActionType.Query
                                               where o.Type == L_CheckType.Department
                                               select String.Format("[{0}][{1}][{2}][True]", o.ProjectCtegory.ID, o.Department.Guid, (int)o.Status));
                if (ieWhere.Any())
                    tmp = ieWhere.ToList();
            }
            else
            {
                IEnumerable<String> ieWhere = (from o in this.AppRoleList
                                               where o.Action == L_ActionType.Query
                                               where o.Type == L_CheckType.Department
                                               select String.Format("[{0}][{1}][{2}][False]", o.ProjectCtegory.ID, o.Department.Guid, (int)o.Status));
                if (ieWhere.Any())
                    tmp = ieWhere.ToList();
            }
            return tmp;
        }

        public List<String> GetQueryWhereByUser(Boolean His)
        {
            List<String> tmp = new List<string>();

            if (His)
            {
                IEnumerable<String> ieWhere = (from o in this.HisRoleList
                                               where o.Action == L_ActionType.Query
                                               where o.Type == L_CheckType.User
                                               select String.Format("[{0}][{1}][{2}][True]", o.ProjectCtegory.ID, o.PIC.Guid, (int)o.Status));
                if (ieWhere.Any())
                    tmp = ieWhere.ToList();

            }
            else
            {
                IEnumerable<String> ieWhere = (from o in this.AppRoleList
                                               where o.Action == L_ActionType.Query
                                               where o.Type == L_CheckType.User
                                               select String.Format("[{0}][{1}][{2}][False]", o.ProjectCtegory.ID, o.PIC.Guid, (int)o.Status));
                if (ieWhere.Any())
                    tmp = ieWhere.ToList();

            }

            return tmp;
        }

        public List<String> GetQueryWhereByDep()
        {
            List<String> tmp = new List<string>();

            IEnumerable<String> ieWhere = (from o in this.AppRoleList
                                           where o.Action == L_ActionType.Query
                                           where o.Type == L_CheckType.Department
                                           select String.Format("[{0}][{1}][{2}]", o.ProjectCtegory.ID, o.Department.Guid, (int)o.Status));
            if (ieWhere.Any())
                tmp = ieWhere.ToList();

            return tmp;
        }

        public List<String> GetQueryWhereByUser()
        {
            List<String> tmp = new List<string>();

            IEnumerable<String> ieWhere = (from o in this.AppRoleList
                                           where o.Action == L_ActionType.Query
                                           where o.Type == L_CheckType.User
                                           select String.Format("[{0}][{1}][{2}]", o.ProjectCtegory.ID, o.PIC.Guid, (int)o.Status));
            if (ieWhere.Any())
                tmp = ieWhere.ToList();

            return tmp;
        }
    }

    #endregion

    #region Item.Menu

    public class MenuViewItem
    {
        public MenuViewItem(List<md_Page> PL, String Url)
        {
            M = new List<MenuLv1>();

            IEnumerable<md_Page> ieL1 = from x in PL where x.Lv == 1 where x.IsMenu select x;
            if (ieL1.Any())
            {
                List<md_Page> L1 = ieL1.ToList();
                foreach (md_Page aL1 in L1)
                {
                    MenuLv1 ML1 = new MenuLv1();
                    ML1.P = aL1;

                    IEnumerable<md_Page> ieL2 = from x in PL where x.Lv == 2 where x.Lv1 == aL1.PageID where x.IsMenu select x;
                    if (ieL2.Any())
                    {
                        List<md_Page> L2 = ieL2.ToList();
                        foreach (md_Page aL2 in L2)
                        {
                            MenuLv2 ML2 = new MenuLv2();
                            ML2.P = aL2;

                            IEnumerable<md_Page> ieL3 = from x in PL where x.Lv == 3 where x.Lv2 == aL2.PageID where x.IsMenu select x;
                            if (ieL3.Any())
                            {
                                List<md_Page> L3 = ieL3.ToList();
                                foreach (md_Page aL3 in L3)
                                {
                                    MenuLv3 ML3 = new MenuLv3();
                                    ML3.P = aL3;

                                    if (aL3.PageUrl != null) { ML3.Select = (Url.Contains(aL3.PageUrl) && !String.IsNullOrEmpty(aL3.PageUrl.Trim())); }
                                    if (ML3.Select) { ML2.Select = true; }

                                    ML2.C.Add(ML3);
                                }

                                if (ML2.C.Count > 0) { ML2.C[ML2.C.Count - 1].Last = true; }
                            }

                            if (ML2.Select) { ML1.Select = true; }

                            ML1.C.Add(ML2);
                        }

                        if (ML1.C.Count > 0) { ML1.C[ML1.C.Count - 1].Last = true; }
                    }
                    
                    this.M.Add(ML1);
                }

                if (M.Count > 0) { M[M.Count - 1].Last = true; }
            }
        }

        public List<MenuLv1> M { get; set; }
    }


    public class MenuLv1
    {
        public MenuLv1()
        {
            this.P = new md_Page();
            this.C = new List<MenuLv2>();
            Select = false;
            Last = false;
        }

        public md_Page P { get; set; }

        public List<MenuLv2> C { get; set; }

        public Boolean Select { get; set; }

        public Boolean Last { get; set; }
    }

    public class MenuLv2
    {
        public MenuLv2()
        {
            this.P = new md_Page();
            this.C = new List<MenuLv3>();
            Select = false;
            Last = false;
        }

        public md_Page P { get; set; }

        public List<MenuLv3> C { get; set; }

        public Boolean Select { get; set; }

        public Boolean Last { get; set; }
    }

    public class MenuLv3
    {
        public MenuLv3()
        {
            this.P = new md_Page();
            Select = false;
            Last = false;
        }

        public md_Page P { get; set; }

        public Boolean Select { get; set; }

        public Boolean Last { get; set; }
    }

    #endregion
}

namespace WebSite.Models
{

    #region Models.Base

    public class BaseModel
    {
        public BaseModel()
        {
            Page_Size = 10;
            Page_Index = 0;
            Row_Count = 0;
            PostingEvent = L_PageEvent.Default;
            PostingID = String.Empty;
            U = new UserItem();
        }

        public Int32 Row_Count { get; set; }
        public Int32 Page_Size { get; set; }
        public Int32 Page_Index { get; set; }

        public UserItem U { get; set; }

        public L_PageEvent PostingEvent { get; set; }
        public String PostingID { get; set; }
    }

    #endregion

    #region Models.Home

    public class HomeModel
    {
        public HomeModel()
        {
        }

        public static UserItem Login(string username, string pwd)
        {
            UserItem oTemp = new UserItem();

            DBDataContext DB = new DBDataContext();

            DB.Connection.Open();

            IEnumerable<md_Department> ieDep = from d in DB.md_Departments where d.Status select d;

            IEnumerable<list_BudgetStatus> ieBS = from bs in DB.list_BudgetStatus select bs;

            IEnumerable<list_ProjectCategory> iePC = from pc in DB.list_ProjectCategories select pc;

            IEnumerable<UserItem> ieQuery = from u in DB.md_Users
                                            from d in DB.md_Departments
                                            where u.DepartmentGuid == d.Guid
                                            where u.UserName == username
                                            where u.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "MD5")
                                            where u.Status
                                            select new UserItem()
                                            {
                                                User = u,
                                                Department = new DepartmentItem()
                                                {
                                                     Department = d
                                                }
                                            };

            if (ieQuery.Any())
            {
                oTemp = ieQuery.First();

                IEnumerable<md_Page> iePageLv3 = from p in DB.md_Pages
                                                 from r in DB.md_Roles
                                                 from rp in DB.md_RolePages
                                                 from rr in DB.md_RoleRelations
                                                 where p.PageID == rp.PageID
                                                 where r.Guid == rp.RoleGuid
                                                 where r.Guid == rr.RoleGuid
                                                 where rr.UserGuid == oTemp.User.Guid
                                                 where p.Lv == 3
                                                 select p;

                if (iePageLv3.Any())
                    oTemp.PageList = iePageLv3.Distinct().ToList();

                IEnumerable<md_Page> iePageLv2 = from p in DB.md_Pages
                                                 from p2 in DB.md_Pages
                                                 from r in DB.md_Roles
                                                 from rp in DB.md_RolePages
                                                 from rr in DB.md_RoleRelations
                                                 where p.PageID == rp.PageID
                                                 where r.Guid == rp.RoleGuid
                                                 where r.Guid == rr.RoleGuid
                                                 where rr.UserGuid == oTemp.User.Guid
                                                 where p.Lv == 3
                                                 where p2.Lv == 2
                                                 where p2.PageID == p.Lv2
                                                 select p2;

                if (iePageLv2.Any())
                    oTemp.PageList.AddRange(iePageLv2.Distinct());

                IEnumerable<md_Page> iePageLv1 = from p in DB.md_Pages
                                                 from p1 in DB.md_Pages
                                                 from r in DB.md_Roles
                                                 from rp in DB.md_RolePages
                                                 from rr in DB.md_RoleRelations
                                                 where p.PageID == rp.PageID
                                                 where r.Guid == rp.RoleGuid
                                                 where r.Guid == rr.RoleGuid
                                                 where rr.UserGuid == oTemp.User.Guid
                                                 where p.Lv == 3
                                                 where p1.Lv == 1
                                                 where p1.PageID == p.Lv1
                                                 select p1;

                if (iePageLv1.Any())
                    oTemp.PageList.AddRange(iePageLv1.Distinct());

                IEnumerable<md_Role> ieRole = from rr in DB.md_RoleRelations
                                              from r in DB.md_Roles
                                              where rr.RoleGuid == r.Guid
                                              where rr.UserGuid == oTemp.User.Guid
                                              select r;

                if (ieRole.Any())
                    oTemp.RoleList = ieRole.ToList();

                //oTemp.IsOnlyNormal = (ieRole.Where(o => o.RoleID == (int)Role.NORMAL).Any() && ieRole.Count() == 1);

                IEnumerable<md_SubRole> ieSubRole = from rr in DB.md_RoleRelations
                                                    from r in DB.md_Roles
                                                    from sr in DB.md_SubRoles
                                                    where sr.RoleGuid == r.Guid
                                                    where rr.RoleGuid == r.Guid
                                                    where rr.UserGuid == oTemp.User.Guid
                                                    select sr;

                List<SubRoleItem> tmpSubRole = new List<SubRoleItem>();

                List<CheckRightItem> tmpAppRole = new List<CheckRightItem>();
                List<CheckRightItem> tmpHisRole = new List<CheckRightItem>();
                                
                List<md_SubRole> lsSubRole = ieSubRole.ToList();

                foreach (md_SubRole aSubRole in lsSubRole)
                {
                    String[] AS = aSubRole.AuthorityString.Split('-');

                    List<list_ProjectCategory> lsPC = new List<list_ProjectCategory>();
                    List<md_Department> lsDep = new List<md_Department>();

                    if (iePC.Any(o => o.ID == aSubRole.ProjectCategoryID))
                        lsPC.Add(iePC.Where(o => o.ID == aSubRole.ProjectCategoryID).First());
                    else
                        lsPC = iePC.ToList();

                    switch (aSubRole.Type)
                    {
                        case (int)L_SubRoleType.Department:
                            lsDep.Add(ieDep.Where(o => o.Guid == aSubRole.DepartmentGuid).First());
                            break;
                        case (int)L_SubRoleType.SelfDepartment:
                            lsDep.Add(oTemp.Department.Department);
                            break;
                        case (int)L_SubRoleType.AllDepartment:
                            lsDep = ieDep.ToList();
                            break;
                    }
                    
                    foreach (list_ProjectCategory aPC in lsPC)
                    {
                        SubRoleItem TmpSR = new SubRoleItem()
                        {
                            OpenAuth = new SubRole_Open(AS[0]),
                            UploadAuth = new SubRole_Upload(AS[1]),
                            AuditAuth = new SubRole_Approve(AS[2]),
                            InUseAuth = new SubRole_Use(AS[3]),
                            AdjustAuth = new SubRole_Adjust(AS[4]),
                            CompleteAuth = new SubRole_Complete(AS[5]),
                            CloseAuth = new SubRole_Close(AS[6]),
                            HistoryAuth = new SubRole_History(AS[7]),
                            ProjectCtegory = aPC,
                        };

                        switch ((L_SubRoleType)aSubRole.Type)
                        {
                            case L_SubRoleType.Self:
                                TmpSR.Union(ref tmpSubRole, TmpSR, oTemp.User);
                                TmpSR.AddCheck(ref tmpAppRole, ref tmpHisRole, oTemp.User);
                                break;                             
                            case L_SubRoleType.Department:
                            case L_SubRoleType.SelfDepartment:
                            case L_SubRoleType.AllDepartment:
                                foreach (md_Department aDep in lsDep)
                                {
                                    TmpSR.Union(ref tmpSubRole, TmpSR, aDep);
                                    TmpSR.AddCheck(ref tmpAppRole, ref tmpHisRole, aDep);
                                }
                                break;
                        }
                    }
                }

                oTemp.AppRoleList = tmpAppRole;
                oTemp.HisRoleList = tmpHisRole;
                oTemp.SubRoleList = tmpSubRole;
            }

            else
                oTemp = null;

            DB.Connection.Close();

            return oTemp;
        }
    }

    #endregion

    #region Model.Index

    public class IndexModel:BaseModel
    {
        public IndexModel()
        {
            //UploadList = new List<BudgetItem>();
            BudgetList = new List<vw_Budget>();
        }

        //public List<BudgetItem> UploadList { get; set; }

        public List<vw_Budget> BudgetList { get; set; }

        //public static List<BudgetItem> GetUploadList(UserItem U)
        //{
        //    List<BudgetItem> lsBudget = new List<BudgetItem>();

        //    DBDataContext DB = new DBDataContext();

        //    DB.Connection.Open();

        //    IEnumerable<BudgetItem> ieBudget = null;

        //    if (U.IsOnlyNormal)
        //    {
        //        ieBudget =
        //            (from b in DB.wf_Budgets
        //             from p in DB.md_Projects
        //             from d in DB.md_Departments
        //             from e in DB.md_Users
        //             from pc in DB.list_ProjectCategories
        //             from pt in DB.list_ProjcetTypes
        //             where ((pt.ID == p.ProjectTypeID && p.ProjectCategoryID == "4") || new String[] { "1", "2", "3" }.Contains(p.ProjectCategoryID))
        //             where b.ProjectGuid == p.Guid
        //             where p.DepartmentGuid == d.Guid
        //             where p.ProjectPIC == e.Guid
        //             where p.ProjectCategoryID == pc.ID
        //             where ((p.ProjectPIC == U.User.Guid && new String[] { "1", "2", "3" }.Contains(p.ProjectCategoryID)) || 
        //                    (U.PriList.Select(o => o.DepGuid + "-" + o.BgtStatus).Contains(p.DepartmentGuid + "-" + b.BudgetStatus) && p.ProjectCategoryID == "4"))
        //             orderby b.Year descending, pc.ID, pt.ID, p.ProjectCode, d.Guid, p.ProjectPIC
        //             select new BudgetItem()
        //             {
        //                 Budget = b,
        //                 Project = new ProjectItem()
        //                 {
        //                     Project = p,
        //                     Department = d,
        //                     ProjcetPIC = e,
        //                     ProjectCategory = pc,
        //                     ProjcetType = pt,
        //                 }
        //             });
        //    }
        //    else
        //    {
        //        ieBudget =
        //               (from b in DB.wf_Budgets
        //                from p in DB.md_Projects
        //                from d in DB.md_Departments
        //                from e in DB.md_Users
        //                from pc in DB.list_ProjectCategories
        //                from pt in DB.list_ProjcetTypes
        //                where ((pt.ID == p.ProjectTypeID && p.ProjectCategoryID == "4") || new String[] { "1", "2", "3" }.Contains(p.ProjectCategoryID))
        //                where b.ProjectGuid == p.Guid
        //                where p.DepartmentGuid == d.Guid
        //                where p.ProjectPIC == e.Guid
        //                where p.ProjectCategoryID == pc.ID
        //                where U.PriList.Select(o => o.DepGuid + "-" + o.BgtStatus).Contains(p.DepartmentGuid + "-" + b.BudgetStatus)
        //                orderby b.Year descending, pc.ID, pt.ID, p.ProjectCode, d.Guid, p.ProjectPIC
        //                select new BudgetItem()
        //                {
        //                    Budget = b,
        //                    Project = new ProjectItem()
        //                    {
        //                        Project = p,
        //                        Department = d,
        //                        ProjcetPIC = e,
        //                        ProjectCategory = pc,
        //                        ProjcetType = pt,
        //                    }
        //                });
        //    }

        //    if (ieBudget.Any())
        //        lsBudget = ieBudget.ToList();

        //    DB.Connection.Close();

        //    return lsBudget;
        //}

        public List<vw_Budget> GetBudgetList()
        {
            List<vw_Budget> lsBudget = new List<vw_Budget>();

            DBDataContext DB = new DBDataContext();

            DB.Connection.Open();
            
            IEnumerable<vw_Budget> ieQuery = null;

            List<String> lsWbD = U.GetQueryWhereByDep();
            List<String> lsWbU = U.GetQueryWhereByUser();

            ieQuery =
                from b in DB.vw_Budgets
                orderby b.BudgetYear descending, b.DepartmentOrder, b.ProjectCategoryID, b.ProjectTypeID, b.ProjectCode, b.ProjectPICID
                where (b.IsHis.HasValue && !b.IsHis.Value)
                where (lsWbD.Contains("[" + b.ProjectCategoryID + "][" + b.DepartmentID + "][" + b.BudgetStatus + "]") ||
                    lsWbU.Contains("[" + b.ProjectCategoryID + "][" + b.ProjectPICID + "][" + b.BudgetStatus + "]"))
                select b;

            if (ieQuery.Any())
                lsBudget = ieQuery.ToList();

            DB.Connection.Close();

            return lsBudget;
        }
    }

    #endregion
}
